package hell.satan.droidbdf.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import java.io.Serializable;

import hell.satan.droidbdf.R;
import hell.satan.droidbdf.base.BaseActivity;
import hell.satan.droidbdf.utils.LogWrapper;

/**
 * 可用于直接打开Fragment
 * Can be used to open Fragment directly
 *
 * @author Cerberus
 */
@SuppressWarnings("unused")
public class FragmentActivity extends BaseActivity {
    public static final String EXTRA_CLASS = "class";
    public static final String EXTRA_TITLE = "title";
    public static final String EXTRA_BUNDLE = "bundle";

    /**
     * 直接打开Fragment
     * Open Fragment directly
     *
     * @param context 任意上下文，若不是Activity，则会自动加上FLAG_ACTIVITY_NEW_TASK
     *                Any context, FLAG_ACTIVITY_NEW_TASK will add automatically if not Activity
     * @param clazz   想要打开的Fragment
     *                Fragment want to open
     * @param title   Activity的标题
     *                Title of activity
     * @param bundle  传给Fragment的参数
     *                Parameters passed to Fragment
     * @param <T>     必须是{@linkplain Fragment android.support.v4.app.Fragment}，
     *                不能是{@linkplain android.app.Fragment android.app.Fragment}
     *                MUST be {@linkplain Fragment android.support.v4.app.Fragment},
     *                NOT {@linkplain android.app.Fragment android.app.Fragment}
     */
    public static <T extends Fragment> void startFragment(
            Context context, Class<T> clazz, String title, @Nullable Bundle bundle) {
        Intent intent = new Intent(context, FragmentActivity.class);
        // !!!IMPORTANT!!!
        intent.setData(Uri.parse("" + Math.random()));
        intent.putExtra(EXTRA_CLASS, clazz);
        intent.putExtra(EXTRA_TITLE, title);
        intent.putExtra(EXTRA_BUNDLE, bundle);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    /**
     * 直接打开Fragment
     * Open Fragment directly
     *
     * @param context 任意上下文，若不是Activity，则会自动加上FLAG_ACTIVITY_NEW_TASK
     *                Any context, FLAG_ACTIVITY_NEW_TASK will add automatically if not Activity
     * @param clazz   想要打开的Fragment
     *                Fragment want to open
     * @param title   Activity的标题
     *                Title of activity
     * @param <T>     必须是{@linkplain Fragment android.support.v4.app.Fragment}，
     *                不能是{@linkplain android.app.Fragment android.app.Fragment}
     *                MUST be {@linkplain Fragment android.support.v4.app.Fragment},
     *                NOT {@linkplain android.app.Fragment android.app.Fragment}
     * @see #startFragment(Context, Class, String, Bundle)
     */
    public static <T extends Fragment> void startFragment(
            Context context, Class<T> clazz, String title) {
        startFragment(context, clazz, title, null);
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();

        Intent intent = getIntent();
        String title = intent.getStringExtra(EXTRA_TITLE);
        Serializable seri = intent.getSerializableExtra(EXTRA_CLASS);
        Bundle bundle = intent.getBundleExtra(EXTRA_BUNDLE);

        setTitle(title);

        try {
            @SuppressWarnings("unchecked")
            Class<Fragment> clazz = (Class<Fragment>) seri;
            Fragment fragment = clazz.newInstance();
            fragment.setArguments(bundle);

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.lyt_content, fragment);
            fragmentTransaction.commit();
        } catch (ClassCastException | NullPointerException
                | InstantiationException | IllegalAccessException ex) {
            LogWrapper.w(TAG, ex);
        }
    }
}
